package cn.offcn.controller;

import cn.offcn.entity.Blog;
import cn.offcn.service.BlogService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/blog")
public class BlogController {


    @Autowired
    private BlogService blogService;

    @Value("${pageSize}")
    private int pageSize;
    @RequestMapping("/{page}")
    public String forwardPage(@PathVariable("page") String page,Integer bid,Model model){
        model.addAttribute("bid",bid);
        return "blog/"+page;
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageView<Blog> getBlogs(@RequestParam(defaultValue = "1") int currentPage){

       return  blogService.getBlogs(currentPage,pageSize);
    }

    @RequestMapping("/getBlogByBid")
    @ResponseBody
    public Map getBlogByBid(int bid){

       return  blogService.getBlogByBid(bid);
    }

    @RequestMapping("/deleteBlogById")
    @ResponseBody
    public OAResult deleteBlogById(int bid){
        return  blogService.deleteBlogById(bid);
    }

}
